<?php

namespace common\components\helper;

use yii\helpers\ArrayHelper;

/**
 * 数组帮助类
 *
 * Class ArrayHelper
 * @package common\helpers
 */
class SysArrayHelper
{
    /**
     * 递归数组
     *
     * @param array $items
     * @param string $id
     * @param int $pid
     * @param string $pidName
     * @param int $level
     * @return array
     */
    public static function itemsMerge(array $items, $id = "id", $pid = 0, $pidName = 'parentId', $level = 1)
    {
        $arr = array();
        foreach ($items as $v) {
            if ($v[$pidName] == $pid) {

                if ($pid == 0) {
                    $level = 1;
                }

                $v['-'] = self::itemsMerge($items, $id, $v[$id], $pidName, $level + 1);
                $v['level'] = $level;
                $arr[] = $v;
            }
        }

        return $arr;
    }

    /**
     * 传递一个子分类ID返回所有的父级分类
     * getParents
     * @param array $items
     * @param $id
     * @param string $idName
     * @param string $pidName
     * @return array
     */
    public static function getParents(array $items, $id,$idName = 'id',$pidName = 'pid')
    {
        $arr = [];
        foreach ($items as $v) {
            if ($v[$idName] == $id) {
                $arr[] = $v;
                $arr = array_merge(self::getParents($items, $v[$pidName]), $arr);
            }
        }
        return $arr;
    }

    /**
     * 传递一个父级分类ID返回所有子分类ID
     * getChildrenId
     * @param $items
     * @param int $pid
     * @param bool $isSelf 是否包含自己
     * @param string $idName
     * @param string $pidName
     * @return array
     */
    public static function getChildrenId($items, $pid, $isSelf = false, $idName = 'id',$pidName = 'parent_id')
    {
        $arr = $isSelf ? [$pid] : [];
        foreach ($items as $v) {
            if ($v[$pidName] == $pid) {
                $arr[] = $v[$idName];
                $arr = array_merge($arr, self::getChildrenId($items, $v[$idName]));
            }
        }

        return $arr;
    }

    /**
     * 传递一个父级分类ID返回所有子分类
     * getChildren
     * @param array $items
     * @param int $pid
     * @param int $idName
     * @param int $pidName
     * @return array
     */
    public static function getChildren($items, $pid, $idName = 'id',$pidName = 'pid')
    {
        $arr = [];
        foreach ($items as $v) {
            if ($v[$pidName] == $pid) {
                $arr[] = $v;
                $arr = array_merge($arr, self::getChildren($items, $v[$idName]));
            }
        }

        return $arr;
    }

    /**
     * TODO 多维数组排序 未做
     * php二维数组排序 按照指定的key 对数组进行排序
     *
     * @param array $items 将要排序的数组
     * @param string $keys 指定排序的key
     * @param string $type 排序类型 asc | desc
     * @return array
     */
    public static function arraySort($items, $keys, $type = 'asc')
    {
        $count = count($items);
        if ($count <= 1) {
            return $items;
        } else {
            $keysValue = [];
            $newArray = [];

            foreach ($items as $k => $v) {
                $keysValue[$k] = $v[$keys];
            }

            $type == 'asc' ? asort($keysValue) : arsort($keysValue);
            reset($keysValue);

            foreach ($keysValue as $k => $v) {
                $newArray[$k] = $items[$k];
            }

            return $newArray;
        }
    }

    /**
     * 根据级别和数组返回字符串
     * itemsLevel
     * @param $level
     * @param array $items
     * @param $k
     * @return bool|string
     */
    public static function itemsLevel($level, array $items, $k)
    {
        $str = '';
        for ($i = 1; $i < $level; $i++) {
            $str .= '　　';
            if ($i == $level - 1) {
                if (isset($items[$k + 1])) {
                    return $str . "├──";
                }

                return $str . "└──";
            }
        }

        return false;
    }

    /**
     * 必须经过递归才能进行重组为下拉框
     * itemsMergeDropDown
     * @param $items
     * @param $id
     * @return array
     */
    public static function itemsMergeDropDown($items, $id = 'id')
    {
        $arr = [];
        foreach ($items as $k => $item) {
            $arr[] = [
                $id => $item[$id],
                'title' => self::itemsLevel($item['level'], $items, $k) . " " . $item['name'],
            ];

            if (!empty($item['-'])) {
                $arr = ArrayHelper::merge($arr, self::itemsMergeDropDown($item['-'], $id));
            }
        }

        return $arr;
    }

    /**
     * 必须经过递归才能进行重组为 递归表格
     * itemsMergeTreeTable
     * @param array $items
     * @param string $id
     * @return array
     */
    public static function itemsMergeTreeTable($items, $id = 'id',$name='name')
    {
        $arr = [];

        foreach ($items as $k => $item) {

            $item[$name] = self::itemsLevel($item['level'], $items, $k) . " " . $item[$name];

            if (!empty($child = $item['-'])) {
                unset($item['-']);
            }

            $arr[$item['id']] = $item;

            if (isset($child)) {
                $arr = ArrayHelper::merge($arr, self::itemsMergeTreeTable($child, $id,$name));
            }
        }

        return $arr;
    }
}